package com.aviccxzx.smarthome.ConfigModule;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.netutil.msg.MsgCenter;
import com.aviccxzx.smarthome.view.RoundProgressBar;

import java.util.List;
import java.util.Timer;

@SuppressLint("HandlerLeak")
public class DoorAirlinkCountdownActivity extends
        GosConfigModuleBaseActivity implements View.OnClickListener {

    /**
     * The tv Time
     */
    private TextView tvTimer;

    /**
     * The rpb Config
     */
    private RoundProgressBar rpbConfig;

    /**
     * 倒计时
     */
    int secondleft = 60;

    /**
     * The timer
     */
    Timer downtimer;

    ImageView radar_scan_img;
    Animation rotateAnim;
    public static DoorAirlinkCountdownActivity doorCountInstance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gos_airlink_config_countdown);
        doorCountInstance = this;
        initView();
        setRotate();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doorCountInstance = null;
    }

    @Override
    protected void onStart() {
        super.onStart();

//        downtimer = new Timer();
//        downtimer.schedule(new TimerTask() {
//            int i = 70;
//
//            @Override
//            public void run() {
//                i--;
//                if (i == 0) {
//                    if (isForeground(DoorAirlinkCountdownActivity.this)) {
//                        Looper.prepare();
//                        Looper.loop();
//                        finish();
//                    }
//                }
//            }
//        }, 0, 1000);

    }

    //	判断某个界面是否在前台
    public static boolean isForeground(Activity activity) {
        return isForeground(activity, activity.getClass().getName());
    }

    /**
     * 判断某个界面是否在前台
     *
     * @param context   Context
     * @param className 界面的类名
     * @return 是否在前台显示
     */
    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName()))
                return true;
        }
        return false;
    }

    private void initView() {
        ((TextView) findViewById(R.id.title_tv)).setText("正在扫描");

        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.islinking_cancle).setOnClickListener(this);
        radar_scan_img = (ImageView) findViewById(R.id.radar_scan_img);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title_left_view:
                MsgCenter.fireNull("cancle_timer", "");
                finish();
                break;
            case R.id.islinking_cancle:
                MsgCenter.fireNull("cancle_timer", "");
                finish();
                break;
        }
    }

    private void setRotate() {
        // 加载动画
        rotateAnim = AnimationUtils.loadAnimation(this, R.anim.radar_rotate);
        LinearInterpolator lin = new LinearInterpolator();// 匀速旋转
        rotateAnim.setInterpolator(lin);
        radar_scan_img.startAnimation(rotateAnim);
    }

}
